package com.haixinga.mvntemplate.bootstrap;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;

import java.util.Arrays;

/**
 * @Author terrfly
 * @Date 2019/11/7 15:19
 * @Description  spring-boot 主启动程序
 **/

@SpringBootApplication
@MapperScan("com.haixinga.mvntemplate.mapper")
@ComponentScan(basePackages = "com.haixinga.mvntemplate.*" )   //由于MainApplication没有在项目跟目录， 需要配置basePackages属性使得成功扫码所有Spring组件；
public class MainApplication{

    /** main启动函数 **/
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }


    /** fastJson 配置信息 **/
    @Bean
    public HttpMessageConverters fastJsonConfig(){

        //新建fast-json转换器
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

        //fast-json 配置信息
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(config);

        //设置响应的 Content-Type
        converter.setSupportedMediaTypes(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8}));
        return new HttpMessageConverters(converter);
    }

}
